var prefix = "/yw/appraisal";
$(document).ready(function () {
    load();
});

function load() {
    $('#exampleTable')
        .bootstrapTreeTable(
            {
                id: 'showId',
                code: 'id',
                parentCode: 'parentId',
                type: "GET", // 请求数据的ajax类型
                url: prefix + '/list', // 请求数据的ajax的url
                ajaxParams: {sort: 'serialNo', type: ''}, // 请求数据的ajax的data属性
                expandColumn: '1',// 在哪一列上面显示展开按钮
                striped: true, // 是否各行渐变色
                bordered: true, // 是否显示边框
                expandAll: false, // 是否全部展开
                columns: [
                    {
                        title: '统计状态',
                        visible: false,
                        width: '5%',
                        align: 'center', valign: 'center',
                        formatter: function (row) {
                            if (row.moneyType === '0' || row.moneyType === '1') {
                                if (row.countSign === 0) {
                                    return '<a class="btn btn-success btn-sm '
                                        + s_add_h
                                        + '" href="#" mce_href="#" title="计入统计" onclick="countSignIn(\''
                                        + row.id
                                        + '\')"><i class="fa fa-plus"></i></a> ';
                                } else if (row.countSign === 1) {
                                    return '<a class="btn btn-warning btn-sm '
                                        + s_edit_h
                                        + '" href="#" title="移除统计"  mce_href="#" onclick="countSignOut(\''
                                        + row.id
                                        + '\')"><i class="fa fa-remove"></i></a> ';
                                } else {

                                    return '无';
                                }
                            } else {

                                return '无';
                            }
                        }
                    },
                    {
                        title: '指标名称',
                        align: 'left', valign: 'center',
                        width: '55%',
                        formatter: function (row) {
                            return row.showId + '：' + row.appraisalContent;
                        }
                    },
                    {
                        title: '金额',
                        align: 'center', valign: 'center',
                        width: '10%',
                        formatter: function (row) {
                            if (row.moneyType !== '2') {
                                if (row.money === 0) {
                                    return '';
                                } else {
                                    return row.money;
                                }
                            }
                        }
                    },
                    {
                        title: '金额标准',
                        align: 'center', valign: 'center',
                        width: '10%',
                        formatter: function (row) {
                            if (row.moneyType !== '2') {
                                if (row.moneyUnit === '无') {
                                    return '';
                                } else {
                                    return row.moneyUnit;
                                }
                            }

                        }
                    },
                    {
                        title: '金额类型',
                        align: 'center', valign: 'center',
                        width: '10%',
                        formatter: function (row) {
                            if (row.moneyType === '0') {
                                return '<span class="label label-success">奖励</span>';
                            } else if (row.moneyType === '1') {
                                return '<span class="label label-warning">处罚</span>';
                            }

                        }
                    },
                    {
                        title: '操作',
                        align: 'center', valign: 'center',
                        width: '10%',
                        formatter: function (row) {
                            if (row.moneyUnit) {
                                var a = '<a class="btn btn-primary btn-sm '
                                    + s_edit_h
                                    + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
                                    + row.id
                                    + '\')"><i class="fa fa-edit"></i></a> ';
                                var b = '<a class="btn btn-primary btn-sm '
                                    + s_add_h
                                    + '" href="#" mce_href="#" title="添加下级" onclick="add(\''
                                    + row.id + '\'' + ',' + '\'' + row.showId
                                    + '\')"><i class="fa fa-plus"></i></a> ';
                                var c = '<a class="btn btn-warning btn-sm '
                                    + s_remove_h
                                    + '" href="#" title="删除"  mce_href="#" onclick="remove(\''
                                    + row.id
                                    + '\')"><i class="fa fa-remove"></i></a> ';
                                return a + b + c;
                            } else {
                                return '<a class="btn btn-primary btn-sm '
                                    + s_add_h
                                    + '" href="#" mce_href="#" title="添加下级" onclick="add(\''
                                    + row.id + '\'' + ',' + '\'' + row.showId
                                    + '\')"><i class="fa fa-plus"></i></a> ';
                            }

                        }
                    }]
            });
}

function reLoad() {
    load();
}

function typeMange() {
    layer.open({
        type: 2,
        title: '添加一级指标',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        moveOut: true,
        content: prefix + '/typeMange/' // iframe的url
    });

}


function add(id, showId) {
    console.log(id);
    console.log(showId);
    layer.open({
        type: 2,
        title: '二级指标添加',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        content: prefix + '/item/add/' + showId + '/' + id
    });
}

function remove(id) {
    layer.confirm('确定要删除选中的记录？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/item/delete/" + id,
            type: "GET",
            success: function (data) {
                if (data.code === 0) {
                    layer.msg("删除成功");
                    reLoad();
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })
}

function edit(id) {
    layer.open({
        type: 2,
        title: '菜单修改',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area: ['800px', '520px'],
        content: prefix + '/item/edit/' + id
    });
}

function countSignIn(id) {
    layer.confirm('确定要将当前指标加入统计吗？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/item/countSignIn/" + id,
            type: "GET",
            success: function (data) {
                if (data.code === 0) {
                    layer.msg("添加成功");
                    reLoad();
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })
}

function countSignOut(id) {
    layer.confirm('确定要将当前指标移除统计吗？', {
        btn: ['确定', '取消']
    }, function () {
        $.ajax({
            url: prefix + "/item/countSignOut/" + id,
            type: "GET",
            success: function (data) {
                if (data.code === 0) {
                    layer.msg("移除成功");
                    reLoad();
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })

}